package com.sinosoft.builder.generator;

import java.io.File;

import com.sinosoft.builder.config.ILoaderHolder;
import com.sinosoft.builder.config.Logger;
import com.sinosoft.builder.generator.ClassPatternSet.PatternEntry;

/**
 * 所有构造器的基础类 - 组件的java文件、页面等的内置元素
 * 
 * 
 * @author 潘巍（Peter Pan）
 * @since 2010-5-14 下午04:21:28
 */
public abstract class InnerGenerator {

	private ILoaderHolder _config;

	private Logger _log;

	protected ClassPatternSet _patterns = new ClassPatternSet();

	public InnerGenerator(ILoaderHolder config, Logger log) {
		_config = config;
		_log = log;
	}

	/**
	 * @return 获得配置文件
	 */
	public ILoaderHolder getConfig() {
		return this._config;
	}

	/**
	 * @param config
	 *            设置配置文件
	 */
	public void setConfig(ILoaderHolder config) {
		this._config = config;
	}

	/**
	 * @return 返回destDir
	 * @throws GeneratorException
	 */
	public File getDestDir() throws GeneratorException {
		File destDir = _config.getDestDir();
		if (destDir == null) {
			throw new GeneratorException("探测生成文件目录不存在。");
		} else if (destDir.exists() && !destDir.isDirectory()) {
			throw new GeneratorException("探测生成文件目录不是一个目录");
		}
		if (!destDir.exists()) {
			destDir.mkdirs();
		}
		return destDir;
	}

	/**
	 * @return 返回resDir
	 * @throws GeneratorException
	 */
	public File getResDir() throws GeneratorException {
		File resDir = _config.getResDir();
		if (resDir == null) {
			throw new GeneratorException("探测资源文件目录不存在。");
		} else if (resDir.exists() && !resDir.isDirectory()) {
			throw new GeneratorException("探测资源文件目录不是一个目录");
		}
		if (!resDir.exists()) {
			resDir.mkdirs();
		}
		return resDir;
	}

	public Logger getLog() {
		return _log;
	}

	public PatternEntry createExclude() {
		return _patterns.createExclude();
	}

	public PatternEntry createInclude() {
		return _patterns.createInclude();
	}

	public void setExcludes(String arg0) {
		_patterns.setExcludes(arg0);
	}

	public void setIncludes(String arg0) {
		_patterns.setIncludes(arg0);
	}

	/**
	 * @param includes
	 * @param excludes
	 * @param component
	 * @param includeComponent
	 * @return
	 */
	protected boolean isIncludeComponent(String name) {
		boolean includeComponent = _patterns.matchClass(name);
		return includeComponent;
	}
}
